//
//  ALDefine.swift
//  ALCore_Swift
//
//  Created by IdeaBank on 2023/12/21.
//


/// 通过类名、函数名调用类方法
/// - Parameters:
///   - anyCls: 类名
///   - selname: 函数名 <@objc>
public func ALClassSelector(anyCls:AnyClass,selname:String) -> Void {
    let anyClass: AnyClass = anyCls.self
    let sel = Selector(selname)
    guard let method = class_getClassMethod(anyClass, sel) else {
        debugPrint("bad action")
        return
    }
    let imp = method_getImplementation(method)
    typealias ClosureType = @convention(c) (AnyClass?, Selector) -> Void
    let classdoaction : ClosureType = unsafeBitCast(imp, to: ClosureType.self)
    classdoaction(anyCls, sel)

}

/// 通过实例、函数名调用类方法
/// - Parameters:
///   - anyCls: 类名
///   - selname: 函数名 <@objc>
public func ALInstanceSelector(anyCls:NSObject,sel:String) {
    let object = anyCls

    let selector = NSSelectorFromString(sel)
    if object.responds(to: selector) {
        object.perform(selector)
    }else{
        debugPrint("bad action")
        return
    }
}


